package com.aigou.service;

import com.aigou.dao.BrandMapper;
import com.aigou.dao.MenuMapper;
import com.aigou.pojo.Brand;
import com.aigou.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class MenuService {

    Map<String, List<Menu>> cacheMap = new HashMap<>();
    ReentrantLock reentrantLock = new ReentrantLock();

    @Autowired
    MenuMapper menuMapper;
    public List<Menu> findAll() {
        List<Menu> list = cacheMap.get("menuList");
        if (list == null) {
            reentrantLock.lock();
            list = cacheMap.get("brandList");
            if (list == null) {
                //危险代码，只走一次
                list = menuMapper.selectByExample(null);
                cacheMap.put("menuList", list);
            }
            reentrantLock.unlock();
        }
        return list;
    }
}
